using System;
using dnAnalytics.Exceptions;
using NUnit.Framework;
using dnAnalytics.LinearAlgebra;
using dnAnalytics.LinearAlgebra.Solvers.Preconditioners;

namespace dnAnalytics.UnitTests.LinearAlgebra.Solvers.Preconditioners
{
    public abstract class PreconditionerTest
    {
        protected const double epsilon = 1e-10;

        internal SparseMatrix CreateUnitMatrix(int size)
        {
            SparseMatrix matrix = (SparseMatrix)MatrixBuilder.CreateMatrix(size, MatrixType.Sparse);
            for (int i = 0; i < size; i++)
            {
                matrix[i, i] = 2;
            }
            return matrix;
        }

        protected Vector CreateStandardBcVector(int size)
        {
            Vector vector = VectorBuilder.CreateVector(size, VectorType.Dense);
            for (int i = 0; i < size; i++)
            {
                vector[i] = i + 1;
            }
            return vector;
        }

        internal abstract IPreconditioner CreatePreconditioner(SparseMatrix matrix);
        
        protected abstract void CheckResult(IPreconditioner preconditioner, Vector vector, Vector result);

        [Test]
        public void SolveWithUnitMatrixReturningNewVector()
        {
            int size = 10;
            SparseMatrix newMatrix = CreateUnitMatrix(size);
            Vector vector = CreateStandardBcVector(size);

            IPreconditioner preconditioner = CreatePreconditioner(newMatrix);
            preconditioner.CreatePreconditioner();

            Vector result = preconditioner.Solve(vector);

            CheckResult(preconditioner, vector, result);
        }

        [Test]
        public void SolveReturningOldVector()
        {
            int size = 10;
            SparseMatrix newMatrix = CreateUnitMatrix(size);
            Vector vector = CreateStandardBcVector(size);

            IPreconditioner preconditioner = CreatePreconditioner(newMatrix);
            preconditioner.CreatePreconditioner();

            Vector result = VectorBuilder.CreateVector(vector.Count, VectorType.Dense);
            preconditioner.Solve(vector, result);

            CheckResult(preconditioner, vector, result);
        }

        [Test]
        [ExpectedException(typeof(NotConformableException))]
        public void SolveWithVectorWithIncorrectLength()
        {
            int size = 10;
            SparseMatrix newMatrix = CreateUnitMatrix(size);
            Vector vector = CreateStandardBcVector(size);

            IPreconditioner preconditioner = CreatePreconditioner(newMatrix);
            
            Vector result = VectorBuilder.CreateVector(vector.Count + 10, VectorType.Dense);
            preconditioner.Solve(vector, result);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void SolveWithNullVector()
        {
            int size = 10;
            SparseMatrix newMatrix = CreateUnitMatrix(size);
            Vector vector = CreateStandardBcVector(size);

            IPreconditioner preconditioner = CreatePreconditioner(newMatrix);
            
            Vector result = VectorBuilder.CreateVector(vector.Count + 10, VectorType.Dense);
            preconditioner.Solve(null, result);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void SolveWithNullResultVector()
        {
            int size = 10;
            SparseMatrix newMatrix = CreateUnitMatrix(size);
            Vector vector = CreateStandardBcVector(size);

            IPreconditioner preconditioner = CreatePreconditioner(newMatrix);
            
            Vector result = null;
            preconditioner.Solve(vector, result);
        }
    }
}